<?php

namespace app\admin\controller;

use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsOption as GoodsOptionModel;
/**
 * 禅旅控制器
 * Class Goods
 * @package app\index\controller
 */
class Goods extends Base
{
    /**
     * [getIndex  获取商品]
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $model = new GoodsModel;
        $opmodel = new GoodsOptionModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try{
            if(!empty($param['title'])){
                $map['title'] = $param['title'];
            }
            if($param['status'] == 1 || $param['status'] == 2){
                $map['status'] = $param['status'];
            }elseif($param['status'] == 4){
                $map['status'] = ['in' ,'1,2'];
                $map['total'] = ['eq' ,0];
            }

            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map,$this->from, $this->size);
            foreach ($lists as &$v){
                $option = $opmodel->getAllData(['goodsid'=>$v['id'],'status'=>1]);
                $v['rule'] = $option;
            }
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        writelog('查看商品列表',1,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getIndex  添加商品]
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $model = new GoodsModel;
        $opmodel = new GoodsOptionModel;
        $param = input('param.');
        $user = $this->user;
        try{
            $id = $model->addData($param);
            foreach($param['rule'] as &$v){
                $v['goodsid'] = $id;
            }
            $opmodel->addDataAll($param['rule']);
        }catch(\Exception $e){
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        writelog('添加商品--'.$id,1,$user);
        return admin_json(1,'添加成功');
    }
    /**
     * [postedit  修改商品]
     * @return json
     * @author [C。]
     */
    public function postedit()
    {
        $model = new GoodsModel;
        $param = input('param.');
        $user = $this->user;
        try{
            $id = $model->editData($param);
        }catch(\Exception $e){
            return admin_json(-1, '修改失败'.$e->getMessage());
        }
        writelog('编辑商品--'.$id,1,$user);
        return admin_json(1,'修改成功');
    }
    /**
     * [getDel  删除商品]
     * @return json
     * @author [C。]
     */
    public function getDel()
    {
        $model = new GoodsModel;
        $param = input('param.');
        $user = $this->user;
        try{
            $model->editData(['id'=>$param['id'],'status'=>3]);
        }catch(\Exception $e){
            return admin_json(-1, '删除失败'.$e->getMessage());
        }
        writelog('删除商品--'.$param['id'],1,$user);
        return admin_json(1,'删除成功');
    }
}